﻿#ifndef ZFTP_H
#define ZFTP_H

/*****************************************************************************
** ZFtp，基于QFtp进行封装
** 自己维护任务队列，不使用QFtp的任务队列
** 将任务结果作为一个对象返回。
** 采用实现私有化方式进行封装
*****************************************************************************/

#include "ZFtpGlobal.h"
#include "ZFtpExports.h"
#include "ZFtpRequest.h"
#include "ZFtpResponse.h"
#include <QObject>
#include <QSharedPointer>

class QIODevice;

BEGIN_NAMESPACE_ZFTPNS

class ZFtpPrivate;
class ZFTP_API ZFtp : public QObject
{
    Q_OBJECT
    Q_DECLARE_PRIVATE(ZFtp)

public:
    explicit ZFtp(QObject *parent = NULL);
    ~ZFtp();

public:
    /**
     * @brief setFtpServerTextCodec
     *  设置服务器的编码，防止中文乱码
     * @param text_codec_name
     */
    void setFtpServerTextCodec(const QString &text_codec_name);

    /**
     * @brief ftpServerTextCodec
     * @return
     */
    QString ftpServerTextCodec();

#if 0
    /**
     * @brief setAutoReconnect
     *  设置连接断开时是否自动重连
     * @param reconnect
     */
    void setAutoReconnect(bool reconnect);

    /**
     * @brief isAutoReconnect
     *  判断当前是否自动重连
     * @return
     */
    bool isAutoReconnect();
#endif

    /**
     * @brief connectToHost
     *  连接服务器
     * @param request
     * @param response
     */
    QSharedPointer<ConnectToHostResponse> connectToHost(QSharedPointer<ConnectToHostRequest> request);

    /**
     * @brief login
     *  登录Ftp服务器
     * @param request
     * @param response
     */
    QSharedPointer<LoginResponse> login(QSharedPointer<LoginRequest> request);

    /**
     * @brief close
     *  关闭Ftp连接
     * @param request
     * @param response
     */
    QSharedPointer<CloseFtpResponse> close(QSharedPointer<CloseFtpRequest> request);

    /**
     * @brief listDir
     *  列出目录
     * @param request
     * @param response
     */
    QSharedPointer<ListDirResponse> listDir(QSharedPointer<ListDirRequest> request);

    /**
     * @brief mkdir
     *  创建目录
     * @param request
     * @param response
     */
    QSharedPointer<MkDirResponse> mkdir(QSharedPointer<MkDirRequest> request);

    /**
     * @brief rmdir
     *  删除目录
     * @param request
     * @param response
     */
    QSharedPointer<RmDirResponse> rmdir(QSharedPointer<RmDirRequest> request);

    /**
     * @brief cd
     *  进入目录
     * @param request
     * @param response
     */
    QSharedPointer<CdDirResponse> cd(QSharedPointer<CdDirRequest> request);

    /**
     * @brief uploadFile
     *  上传1
     * @param request
     * @param response
     */
    QSharedPointer<UploadFileResponse1> uploadFile(QSharedPointer<UploadFileRequest1> request);

    /**
     * @brief uploadFile
     *  上传2
     * @param request
     * @param response
     */
    QSharedPointer<UploadFileResponse2> uploadFile(QSharedPointer<UploadFileRequest2> request);

    /**
     * @brief uploadFile
     *  上传3
     * @param request
     * @param response
     */
    QSharedPointer<UploadFileResponse3> uploadFile(QSharedPointer<UploadFileRequest3> request);

    /**
     * @brief downloadFile
     *  下载
     * @param request
     * @param response
     */
    QSharedPointer<DownloadFileResponse1> downloadFile(QSharedPointer<DownloadFileRequest1> request);

    /**
     * @brief downloadFile
     *  下载
     * @param request
     * @param response
     */
    QSharedPointer<DownloadFileResponse2> downloadFile(QSharedPointer<DownloadFileRequest2> request);

    /**
     * @brief deleteFile
     *  删除文件
     * @param request
     * @param response
     */
    QSharedPointer<DeleteFileResponse> deleteFile(QSharedPointer<DeleteFileRequest> request);

    /**
     * @brief renameFile
     *  重命名文件
     * @param request
     * @param response
     */
    QSharedPointer<RenameFileResponse> renameFile(QSharedPointer<RenameFileRequest> request);

    /**
     * @brief setTransferMode
     *  设置传输模式
     * @param mode
     */
    void setTransferMode(QFtp::TransferMode mode);

    /**
     * @brief state
     * @return
     */
    QFtp::State state() const;

    /**
     * @brief error
     * @return
     */
    QFtp::Error	error() const;

    /**
     * @brief errorString
     * @return
     */
    QString	errorString() const;

public slots:
    void abort();

private:
    void init();
    void createConnects();

protected:
    ZFtpPrivate* const d_ptr;

signals:
    void sigFtpStateChanged(int state);
    void sigNewTaskAdded(QPointer<ZFtpRequest>, QPointer<ZFtpResponse>);
};

END_NAMESPACE

#endif // ZFTP_H
